package org.example.service.impl;

import org.example.mapper.DormitoryTbMapper;
import org.example.pojo.DormitoryTb;
import org.example.service.api.DormitoryServiceApi;
import org.example.util.JSONResult;
import org.example.util.JSONStats;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 寝室服务相关
 */
@Service
public class DormitoryService implements DormitoryServiceApi<DormitoryTb> {

    //调用寝室Dao
    @Autowired
    DormitoryTbMapper dormitoryTbMapper;

    /**
     * 检查寝室是否存在
     * @param dormitorynname 寝室名字
     * @return
     */
    @Override
    public JSONResult checkDormitoryName(String dormitorynname) {
        JSONResult jsonResult = new JSONResult();

//        DormitoryTb dormitoryTb = new DormitoryTb();
//        dormitoryTb.setDormitoryName(dormitorynname);
        DormitoryTb dormitoryInfo= dormitoryTbMapper.queryDormitoryInfo(dormitorynname);

        System.out.println(dormitoryInfo);


        //设置响应状态码
        if(dormitoryInfo!=null){
            jsonResult.setStatusCode(JSONStats.CHECK_DORMITORY_NAME_OK.getStatusCode());
            jsonResult.setMessage(JSONStats.CHECK_DORMITORY_NAME_OK.getMessage());
            //设置响应体数据
            jsonResult.setData(dormitoryInfo);

        }else {
            //失败的情况下
            jsonResult.setStatusCode(JSONStats.CHECK_DORMITORY_NAME_ERROR.getStatusCode());
            jsonResult.setMessage(JSONStats.CHECK_DORMITORY_NAME_ERROR.getMessage());
        }

        return jsonResult;
    }

    /**
     *
     * 用户充值信息
     * @param userid 用户id
     * @param usermonkey 用户金钱
     * @param dormitoryid 用户寝室id
     * @return
     */
    @Override
    public JSONResult depositMonkey(Integer userid, Integer usermonkey, Integer dormitoryid) {
        JSONResult jsonResult = new JSONResult();
        int rows1= dormitoryTbMapper.depositMonkey(usermonkey,dormitoryid);
        int rows2=dormitoryTbMapper.insertDepositInfo(userid,usermonkey,dormitoryid);


        if(rows1>0 && rows2>0){
            jsonResult.setStatusCode(JSONStats.DESPOIT_MONKEY_OK.getStatusCode());
            jsonResult.setMessage(JSONStats.DESPOIT_MONKEY_OK.getMessage());
        }else{
            //失败
            jsonResult.setStatusCode(JSONStats.DESPOIT_MONKEY_ERROR.getStatusCode());
            jsonResult.setMessage(JSONStats.DESPOIT_MONKEY_ERROR.getMessage());
        }
        return jsonResult;
    }

    /**
     * 寝室信息增加
     * @param dormitoryName 寝室名字
     * @param dormitoryFee 寝室费用
     * @return
     */
    @Override
    public JSONResult adminAddDormitoryInfo(String dormitoryName, Integer dormitoryFee) {
        JSONResult jsonResult = new JSONResult();

        int rows= dormitoryTbMapper.addDormitoryInfo(dormitoryName,dormitoryFee);
        if(rows>0){
            //创建成功
            jsonResult.setStatusCode(JSONStats.ADD_DORMITORY_SUCCESS.getStatusCode());
            jsonResult.setMessage(JSONStats.ADD_DORMITORY_SUCCESS.getMessage());

        }else {
            //创建失败
            jsonResult.setMessage(JSONStats.ADD_DORMITORY_ERROR.getMessage());
            jsonResult.setStatusCode(JSONStats.ADD_DORMITORY_ERROR.getStatusCode());
        }
        return jsonResult;
    }

    /**
     * 修改寝室信息
     * @param dormitoryname 寝室名字
     * @param dormitorymonkey 寝室金钱
     * @param dormitoryid 寝室id
     * @return
     */
    @Override
    public JSONResult adminAlterDormitoryInfo(String dormitoryname, Integer dormitorymonkey, Integer dormitoryid) {
        JSONResult jsonResult =new JSONResult();
        int rows= dormitoryTbMapper.alterDormitoryInfo(dormitoryname,dormitorymonkey,dormitoryid);
        //根据影响行数
        if(rows>0){
            //成功 ， 设置响应信息
            jsonResult.setStatusCode(JSONStats.ALTER_DORMITORY_SUCCESS.getStatusCode());
            jsonResult.setMessage(JSONStats.ALTER_DORMITORY_SUCCESS.getMessage());
        }else {
            //失败  设置失败的相应信息
            jsonResult.setMessage(JSONStats.ALTER_DORMITORY_ERROR.getMessage());
            jsonResult.setStatusCode(JSONStats.ALTER_DORMITORY_ERROR.getStatusCode());
        }
        return jsonResult;
    }

    /**
     * 寝室信息删除
     * @param dormitoryname 寝室名字
     * @return
     */
    @Override
    public JSONResult adminDeleteDormitoryInfo(String dormitoryname) {
        JSONResult jsonResult = new JSONResult();

        int rows=dormitoryTbMapper.deleteDormitoryInfo(dormitoryname);

        if(rows>0){
            jsonResult.setStatusCode(JSONStats.DELETE_DORMITORY_SUCCESS.getStatusCode());
            jsonResult.setMessage(JSONStats.DELETE_DORMITORY_SUCCESS.getMessage());
        }else {
            jsonResult.setStatusCode(JSONStats.DELETE_DORMITORY_ERROR.getStatusCode());
            jsonResult.setMessage(JSONStats.DELETE_DORMITORY_ERROR.getMessage());
        }
        return jsonResult;
    }
}
